package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.ZqzrlbDao;
import com.mchange.v1.util.StringTokenizerUtils;


/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月16日 上午9:34:42
 * @功能：债权收益界面
 */

@Repository
public class ZqzrlbDaoImpl extends P2PJdbcDao implements ZqzrlbDao {
	
	Logger logger = Logger.getLogger(ZqshcgDaoImpl.class);

	/**
	 * @author zhai
	 * 获取查询的数据集合
	 */
	@Override
	public List queryZqzrlbList(JSONObject params) {

		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		
		String xmmct = params.optString("xmmct");
		String jkrt = params.optString("jkrt");
		String dfridt = params.optString("dfridt");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("xmmct", xmmct);
		paramMap.put("jkrt", jkrt);
		paramMap.put("dfridt", dfridt);
		sql.append(" SELECT ID,XMMC,SJSYL,JE,QX,JD,FBSJ,ZT,JKR,DFRID,JKXMJJ,JKYT,DBSM,MJQ,JXRQ  ");
		sql.append(" ,date_format(FBSJ,'%Y-%m-%d %h:%i:%s') FBSJ from t_zhfw_zqzrlb");
		sql.append(" where 1=1 ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND FBSJ >= :startDate ");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND FBSJ <= :endDate ");
			paramMap.put("endDate",endDate);
		}
		if(StringUtils.isNotEmpty(xmmct)){
			sql.append(" AND XMMC like "+ "'%"+xmmct+"%'");
		}
		if(StringUtils.isNotEmpty(jkrt)){
			sql.append(" AND JKR like "+ "'%"+jkrt+"%'");
		}
		if(StringUtils.isNotEmpty(dfridt)){
			sql.append(" AND DFRID like "+ "'%"+dfridt+"%'");
		}
		
		sql.append(" order by FBSJ desc LIMIT :START,:LIMIT");
		return this.getNamedParameterJdbcTemplate().queryForList(
				sql.toString(), paramMap);

	}

	/**
	 * @author zhai
	 * 获取债权转让的数据个数
	 */
	@SuppressWarnings("deprecation")
	@Override
	public int getZqzrlbCount(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		String xmmct = params.optString("xmmct");
		String jkrt = params.optString("jkrt");
		String dfridt = params.optString("dfridt");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("xmmct", xmmct);
		paramMap.put("jkrt", jkrt);
		paramMap.put("dfridt", dfridt);
		sql.append(" select count(*) CT from t_zhfw_zqzrlb");
		sql.append(" where 1=1 ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND FBSJ >= :startDate ");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND FBSJ <= :endDate ");
			paramMap.put("endDate",endDate);
		}
		if(StringUtils.isNotEmpty(xmmct)){
			sql.append(" AND XMMC like "+ "'%"+xmmct+"%'");
		}
		if(StringUtils.isNotEmpty(jkrt)){
			sql.append(" AND JKR like "+ "'%"+jkrt+"%'");
		}
		if(StringUtils.isNotEmpty(dfridt)){
			sql.append(" AND DFRID like "+ "'%"+dfridt+"%'");
		}
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}
	
	private int getZqzrlbMaxId(){
		StringBuffer sql = new StringBuffer();
		Map<String,String> paramMap = new HashMap<>();
		sql.append(" select case when max(convert(id,signed)) is null then 0 ");
		sql.append(" else max(convert(id,signed)) end maxId ");
		sql.append(" from t_zhfw_zqzrlb ");
		logger.info("getZqzrlbMaxId sql is " + sql.toString());
		return this.getNamedParameterJdbcTemplate().queryForObject(sql.toString(), paramMap, Integer.class);
	}

	@Override
	public boolean insertZqzrlb(Map<String, Object> zqshcgMap) {
		boolean result = false;
		if(null != zqshcgMap){
			int maxId = this.getZqzrlbMaxId();
			String CQID = MapUtils.getString(zqshcgMap, "ID", "");
			String XMMC = MapUtils.getString(zqshcgMap, "XMMC", "");
			String SJSYL = MapUtils.getString(zqshcgMap, "SJSYL", "");
			String JE = MapUtils.getString(zqshcgMap, "JE", "");
			String QX = MapUtils.getString(zqshcgMap, "QX", "");
			String JD = MapUtils.getString(zqshcgMap, "JD", "0");//默认进度0
			String FBSJ = MapUtils.getString(zqshcgMap, "FBSJ", "");
			String ZT = MapUtils.getString(zqshcgMap, "ZT", "1");//默认标状态：1-招标中
			String JKR = MapUtils.getString(zqshcgMap, "JKR", "");
			String DFRID = MapUtils.getString(zqshcgMap, "DFRID", "");
			String JKXMJJ = MapUtils.getString(zqshcgMap, "JKXMJJ", "");
			String JKYT = MapUtils.getString(zqshcgMap, "JKYT", "");
			String DBSM = MapUtils.getString(zqshcgMap, "DBSM", "");
			String MJQ = MapUtils.getString(zqshcgMap, "MJQ", "");
			String JXRQ = MapUtils.getString(zqshcgMap, "JXRQ", "");
			String SJSYL_SHOW = MapUtils.getString(zqshcgMap, "SJSYL_SHOW", "");
			String XM_TYPE = MapUtils.getString(zqshcgMap, "XM_TYPE", "");
			String HK_TYPE = MapUtils.getString(zqshcgMap, "HK_TYPE", "");
			String QX_MONTH = MapUtils.getString(zqshcgMap, "QX_MONTH", "");
			String XM_ZC_TYPE = MapUtils.getString(zqshcgMap, "XM_ZC_TYPE", "");//项目资产类型
			String XM_PGJZ = MapUtils.getString(zqshcgMap, "XM_PGJZ", "");//项目评估价值
			String XM_SYED = MapUtils.getString(zqshcgMap, "XM_SYED", "");//项目授信额度(万)
			//新增借款人信息
			String jkrCsnf = MapUtils.getString(zqshcgMap, "JKR_CSNF", "");//出生年份
			String jkrZgxl =  MapUtils.getString(zqshcgMap, "JKR_ZGXL", "");
			String jkrHyzk =  MapUtils.getString(zqshcgMap, "JKR_HYZK", "");
			String jkrZnqk =  MapUtils.getString(zqshcgMap, "JKR_ZNQK", "");
			String jkrHjdz =   MapUtils.getString(zqshcgMap, "JKR_HJDZ", "");
			String jkrZcSrsp =  MapUtils.getString(zqshcgMap, "JKR_ZC_SRSP", "");
			String jkrZcFcqk =  MapUtils.getString(zqshcgMap, "JKR_ZC_FCQK", "");
			String jkrZcXykzs =  MapUtils.getString(zqshcgMap, "JKR_ZC_XYKZS", "");
			String jkrZcXyked =  MapUtils.getString(zqshcgMap, "JKR_ZC_XYKED", "");
			String jkrDwHy =  MapUtils.getString(zqshcgMap, "JKR_DW_HY", "");
			String jkrDwXz =  MapUtils.getString(zqshcgMap, "JKR_DW_XZ", "");
			String jkrDwDz =  MapUtils.getString(zqshcgMap, "JKR_DW_DZ", "");
			String jkrDwZw =   MapUtils.getString(zqshcgMap, "JKR_DW_ZW", "");
			String jkrDwRzsj =  MapUtils.getString(zqshcgMap, "JKR_DW_RZSJ", "");
			
			StringBuffer sql = new StringBuffer();
			Map<String,Object> paramMap = new HashMap<>();
			sql.append(" INSERT INTO t_zhfw_zqzrlb ");
			sql.append(" ( ID, XMMC, SJSYL, JE, QX, JD, FBSJ ");
			sql.append("	,ZT, JKR, DFRID, JKXMJJ, JKYT, DBSM, MJQ, JXRQ, SJSYL_SHOW,XM_TYPE,HK_TYPE,REF_CGID,QX_MONTH ");
			sql.append(" ,XM_ZC_TYPE,XM_PGJZ,XM_SYED ");
			sql.append(" ,JKR_CSNF,JKR_ZGXL,JKR_HYZK,JKR_ZNQK,JKR_HJDZ,JKR_ZC_SRSP,JKR_ZC_FCQK,JKR_ZC_XYKZS,JKR_ZC_XYKED ");
			sql.append(" ,JKR_DW_HY,JKR_DW_XZ,JKR_DW_DZ,JKR_DW_ZW, JKR_DW_RZSJ )");
			
			sql.append(" VALUES ");
			sql.append(" (:ID, :XMMC, :SJSYL, :JE, :QX, :JD, :FBSJ ");
			sql.append(" ,:ZT, :JKR, :DFRID, :JKXMJJ, :JKYT, :DBSM, :MJQ, :JXRQ, :SJSYL_SHOW, :XM_TYPE, :HK_TYPE,:REF_CGID,:QX_MONTH ");
			sql.append(" ,:XM_ZC_TYPE,:XM_PGJZ,:XM_SYED ");
			sql.append(" ,:JKR_CSNF,:JKR_ZGXL,:JKR_HYZK,:JKR_ZNQK,:JKR_HJDZ,:JKR_ZC_SRSP,:JKR_ZC_FCQK,:JKR_ZC_XYKZS,:JKR_ZC_XYKED ");
			sql.append(" ,:JKR_DW_HY,:JKR_DW_XZ,:JKR_DW_DZ,:JKR_DW_ZW, :JKR_DW_RZSJ )");
			maxId++;
			paramMap.put("ID", String.valueOf(maxId));
			paramMap.put("XMMC", XMMC);
			paramMap.put("SJSYL", SJSYL);
			paramMap.put("JE", JE);
			paramMap.put("QX", QX);
			paramMap.put("JD", "0");
			paramMap.put("FBSJ", FBSJ);
			paramMap.put("ZT", "1");
			paramMap.put("JKR", JKR);
			paramMap.put("DFRID", DFRID);
			paramMap.put("JKXMJJ", JKXMJJ);
			paramMap.put("JKYT", JKYT);
			paramMap.put("DBSM", DBSM);
			paramMap.put("MJQ", MJQ);
			paramMap.put("JXRQ", JXRQ);
			paramMap.put("SJSYL_SHOW", SJSYL_SHOW);
			paramMap.put("XM_TYPE", XM_TYPE);
			paramMap.put("HK_TYPE", HK_TYPE);
			paramMap.put("REF_CGID", CQID);
			paramMap.put("QX_MONTH", QX_MONTH);
			paramMap.put("XM_ZC_TYPE", XM_ZC_TYPE);
			paramMap.put("XM_PGJZ", XM_PGJZ);
			paramMap.put("XM_SYED", XM_SYED);
			//新增借款人信息
            paramMap.put("JKR_CSNF", jkrCsnf);
            paramMap.put("JKR_ZGXL", jkrZgxl);
            paramMap.put("JKR_HYZK", jkrHyzk);
            paramMap.put("JKR_ZNQK", jkrZnqk);
            paramMap.put("JKR_HJDZ", jkrHjdz);
            paramMap.put("JKR_ZC_SRSP", jkrZcSrsp);
            paramMap.put("JKR_ZC_FCQK", jkrZcFcqk);
            paramMap.put("JKR_ZC_XYKZS", jkrZcXykzs);
            paramMap.put("JKR_ZC_XYKED", jkrZcXyked);
            paramMap.put("JKR_DW_HY", jkrDwHy);
            paramMap.put("JKR_DW_XZ", jkrDwXz);
            paramMap.put("JKR_DW_DZ", jkrDwDz);
            paramMap.put("JKR_DW_ZW", jkrDwZw);
            if(StringUtils.isEmpty(jkrDwRzsj)){
            	paramMap.put("JKR_DW_RZSJ", null);
            }else{
            	paramMap.put("JKR_DW_RZSJ", jkrDwRzsj);
            }
			
			
			
			logger.info("insertZqzrlb sql is : " + sql.toString());
			logger.info("insertZqzrlb paramMap is : " + paramMap);
			this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
			
			//发标时选择分档类别
			HashMap zqfdUseMap = new HashMap<>();
			zqfdUseMap.put("ZQ_ID",  String.valueOf(maxId));
			zqfdUseMap.put("FD_TYPE_ID",  MapUtils.getString(zqshcgMap, "FD_TYPE_ID", ""));
			this.saveZqfdUse(zqfdUseMap);
			result = true;
		}
		return result;
	}

	@Override
	public boolean saveZqfdUse(Map<String, Object> zqshcgMap) {
		if(null!=zqshcgMap){
			String ID = MapUtils.getString(zqshcgMap, "ZQ_ID", "");
			String FD_TYPE_ID = MapUtils.getString(zqshcgMap, "FD_TYPE_ID", "");
			if(StringUtils.isNotEmpty(FD_TYPE_ID)){
				if(StringUtils.isNotEmpty(ID)){
					StringBuffer sql = new StringBuffer();
					Map<String,String> paramMap = new HashMap<>();
					sql.append(" INSERT INTO t_zhfw_zqfd_use(ID,ZQFD_ID,ZQ_ID) ");
					sql.append(" VALUES(UUID(),:ZQFD_ID,:ZQ_ID) ");
					paramMap.put("ZQFD_ID", FD_TYPE_ID);
					paramMap.put("ZQ_ID", ID);
					logger.info("saveZqfdUse sql is " + sql.toString());
					logger.info("saveZqfdUse paramMap is " + paramMap);
					this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
					return true;
				}else{
					logger.info("saveZqfdUse 债权ID获取失败");
				}
			}else{
				logger.info("saveZqfdUse 债权分档类别 为空，未使用分档类别");
			}
		}
		return false;
	}


}
